package com.example.vo

import android.content.ContentValues
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import com.example.vo.Controller.ActivityCollector.finishAll
import com.example.vo.Controller.BaseActivity
import com.example.vo.Data.DB_History
import com.example.vo.Data.History

class ResultActivity : BaseActivity() {
    private var lastClickTime = 0L

    private val FAST_TIME = 1000

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)
        val item = intent.getStringExtra("item")
        val maxnum = intent.getStringExtra("maxnum")
        val rightnum = intent.getStringExtra("rightnum")
        val confirm = intent.getStringExtra("confirm")?.toInt()
        val onum = "0"
        val rightnum_ = "0"

        val toolbar: Toolbar = findViewById(R.id.toolbar_r)
        toolbar.title = "测试结果"
        setSupportActionBar(toolbar)

        val result_item: TextView = findViewById(R.id.result_item)

        var resultnum_ = 0
        val ratio: Double
        val result_ratio:TextView = findViewById(R.id.result_ratio)
        if (rightnum != null && maxnum != null){
            if (confirm == 0){
                result_item.text = item + "词汇量"
                ratio = ((rightnum.toDouble())/(maxnum.toDouble()))
                if (item == "小学"){
                    resultnum_ = (ratio*616).toInt()
                    result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n" + "题库总词汇量:" + "616"
                }
                else if (item == "初中"){
                    resultnum_ = (ratio*1420).toInt()
                    result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n" + "题库总词汇量:" + "1420"
                }
                else if (item == "高中"){
                    resultnum_ = (ratio*3668).toInt()
                    result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n" + "题库总词汇量:" + "3668"
                }
                else if (item == "大学四级"){
                    resultnum_ = (ratio*3486).toInt()//1162
                    result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n" + "题库总词汇量:" + "3486"
                }
                else if (item == "大学六级"){
                    resultnum_ = (ratio*3684).toInt() //1228
                    result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n" + "题库总词汇量:" + "3684"
                }
                else if (item == "考研"){
                    resultnum_ = (ratio*5364).toInt() //1341
                    result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n" + "题库总词汇量:" + "5364"
                }
            }
            else{
                result_item.text = "词汇量"
                ratio = ((rightnum.toDouble())/(maxnum.toDouble()))
                resultnum_ = (ratio*9119).toInt()
                result_ratio.text = "正确题数:" + rightnum + "\n正确率:" + (ratio*100).toInt() + ".00%\n"
//                + "题库总词汇量:" + "18238"
            }
        }

        val resultnum: Array<String> = arrayOf("0", "0", "0", "0")
        if (rightnum != null) {
            for (i in 0 until resultnum_.toString().length) {
                resultnum[3-i] = resultnum_.toString()[(resultnum_.toString().length - 1 - i)].toString()
            }
        }
        val result_num_1: TextView = findViewById(R.id.result_num_1)
        val result_num_2: TextView = findViewById(R.id.result_num_2)
        val result_num_3: TextView = findViewById(R.id.result_num_3)
        val result_num_4: TextView = findViewById(R.id.result_num_4)
        result_num_1.text = resultnum[0]
        result_num_2.text = resultnum[1]
        result_num_3.text = resultnum[2]
        result_num_4.text = resultnum[3]

        val re_test: Button = findViewById(R.id.re_test)
        val return_main: Button = findViewById(R.id.return_main)
        re_test.setBackgroundColor(Color.parseColor("#EDEDED"))
        return_main.setBackgroundColor(Color.WHITE)

        re_test.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime >= FAST_TIME) {
//                if (item != null && rightnum!= null) {
//                    savehistory(item,rightnum,"4.24")
//                }
                val intent = Intent(this, TestActivity::class.java).apply {
                    putExtra("item", item)
                    putExtra("num", maxnum.toString())
                    putExtra("maxnum",maxnum.toString())
                    putExtra("rightnum",rightnum_)
                    putExtra("onum",onum)
                    putExtra("confirm",confirm.toString())
                }
                startActivity(intent)
                finish()
                lastClickTime = System.currentTimeMillis()
            }
        }
        return_main.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime >= FAST_TIME) {
//                if (item != null && rightnum!= null) {
//                    savehistory(item,rightnum,"4.24")
//                }
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finishAll() //这里使用finishall是为了把栈中原有的MainFragment销毁，避免出现两个MainFragment的情况
                lastClickTime = System.currentTimeMillis()
            }
        }
    }

    private fun savehistory(item:String,result:String,time:String){
        val dbhelper = DB_History(this,"History.db",1)
        val db = dbhelper.writableDatabase
        val value = ContentValues().apply {
            put("item",item)
            put("result",result)
            put("time",time)
        }
        db.insert("history",null,value)
    }
}